var app = getApp()
var http = require('../../utils/http.js') 
var that
// var browser = require('../../utils/s_live/browser.js')  
// var M3U8 = require('../../utils/s_live/s_src/M3U8.js') 
// var AESDecrypt = require('../../utils/s_live/s_src/AESDecrypt.js') 
// var TSDemuxer = require('../../utils/s_live/s_src/TSDemuxer.js') 
// var player = require('../../utils/s_live/s_src/player.js')
// var SPSParser = require('../../utils/s_live/s_src/SPSParser.js')
// var MP4Muxer = require('../../utils/s_live/s_src/MP4Muxer.js') 

Page({ 
  data: {
    music_icon:"../../images/music_icon.jpg", 
    pub_selected:null,
    selected_idx: -1,   //被选中的猫咪序号，用于设置被选中效果 
    selectedpub_idx: 't',  //同上
    text: "This is page data.",
    flag:1,
    no_care_hidden:true,//该客户没猫托养时显示提示托养界面
    care_cat_nick:null,         //被托养的猫（几只猫）昵称
    care_cat_photo:null,          //头像
    cat_all_camera:null,          //所有托养猫的摄像头集合
    // tt:"http://zmmy.b-bri.cn/langchao/test.m3u8",
    p1:null,         //公共区1/2/3（几个区）
    p2:null,
    p3:null,
    camera_address:0,      //当前猫房间的所有摄像头地 
    // video_address: "https://wxzb.b-bri.cn/maomijiudian/57.m3u8",
    address_index:0,     //当前地址在当前摄像头组的序列号
    now_adr:'', //当前摄像头，用于数据分析
    mute: false,        //视频是否静音
    h:0,
    pub: ['1楼公共区', '2楼公共区','洗澡区'],
    pub_img: ['../../images/public001.png', '../../images/public002.png','../../images/public003.png'],
    shopIndex: [],
    objectShop: []   
  },
  onLoad: function () {
    that = this  ;
    that.get_shop_msg(); //获取店铺信息
    this.setData({
      index: wx.getStorageSync('shop_index')
    });

     
  
    app.getUserInfo(function (res) {   //判断有无授权获取信息
        app.req("tuoyang.html", { "openid": app.data.openid,"shop_id":wx.getStorageSync('shop_id') }, function (res) {//获取该用户托养的猫的信息
          if (res.status == "success") {
            // console.log(res)
            that.setData({
              care_cat_nick: res.nick,
              care_cat_photo: res.cat_photo,
              cat_all_camera: res.camera,
              p1: res.one,      //公共区摄像头
              p2: res.two,
              p3: res.three,
            })
            if (res.count == 0)//该用户尚未托养猫咪，提示托养
            {
              // that.setData({ no_care_hidden: false });//没猫提示托养不给公共区视频
              app.toast_up("您尚未托养猫咪")
            }else{
              // that.setData({   //设置初始默认选中地址组为第一个
              //   camera_address: that.data.cat_all_camera[0].split(','),
              //   now_adr: that.data.cat_all_camera[0].split(','),
              // });
                var cc1=[]
                for (var i = 0; i < res.camera.length;i++){
                  var cc2 = res.camera[i].split(',')
                  for(var j=0;j<cc2.length;j++){
                    cc1.push(cc2[j]);
                  }
                }
                cc1=that.unique(cc1); //去重后
                var cc3='';
                for (var k = 0; k < cc1.length; k++) {//再转为字符串
                  cc3 += cc1[k]+',';
                }
                cc3 = cc3.substring(0,cc3.length-1);
                var cc4=[];
                cc4.push(cc3);
                that.setData({ care_cat_nick: ["猫房间"], care_cat_photo: ["../../images/cat_room.png"], cat_all_camera: cc4})
                // console.log(cc4)
            }
          } else if (res.status == "error") {
            app.toast_up(res.mess)
          } else {
            app.modal("错误提示", "网络错误，是否重试？", function (re) {
              if (re) {
                that.onLoad()
              }
            })
          }
        })
     },function (res) { 
       wx.navigateTo({
         url: '../home/home',
       })
     });//判断用户有没有授权，请求授权
    this.setData({
      h: wx.getSystemInfoSync().windowHeight 
    });
  },

  bindPickerChange: function (e) {
    that = this;
    console.log('picker发送选择改变，携带值为', e.detail.value)
    var shop_id = that.data.shopIndex[e.detail.value].id;
    wx.setStorageSync('shop_id', shop_id);
    this.setData({
      index: e.detail.value
    });
    wx.setStorageSync('shop_index', e.detail.value);

    wx.reLaunch({
      url: 'my_cares'
    })
  },
  onReady: function () {
    // Do something when page ready.
  },
  onShow: function () {
    app.check_vs(); //检查版本
   this.onLoad();
  },
  onHide: function () {
    // Do something when page hide.
  },
  to_ser:function(){
    app.data.to_ser = 1;
    wx.switchTab({
      url: '../home/home'
    })
  },
  unique:function (arr){
    var newArr = [arr[0]];
    for(var i= 1;i<arr.length;i++){
  　　　　if (newArr.indexOf(arr[i]) == -1) {
            newArr.push(arr[i]);
          }
    }
    return newArr;
   },
  sound:function(){  //点击静
    this.setData({ mute:!this.data.mute});
    if (this.data.mute)
    {
      this.setData({ music_icon:'../../images/music_icon_off.png'});
    }else{
      this.setData({ music_icon: '../../images/music_icon.jpg' });
    }
  },
  switch_video: function () {  //点击切换
    
    if (parseInt(that.data.address_index) + 1 < that.data.camera_address.length)
    {
      that.setData({ address_index: parseInt(that.data.address_index)+1 });
    } else { that.setData({ address_index: 0 });}
    // console.log(that.data.camera_address[that.data.address_index]);
    that.setData({ now_adr: that.data.camera_address[that.data.address_index]})
  },
  watch:function(e){                  //点击猫咪头像选择视频
    that.setData({
      selected_idx: e.currentTarget.id,    //设置选中效果
      selectedpub_idx:'t',
      camera_address: that.data.cat_all_camera[e.currentTarget.id].split(',')});//设置选中地址组
    that.setData({ address_index: 0, now_adr: that.data.camera_address[0] });//设置默认选中地址为第一个
    // console.log(that.data.camera_address);
  },
  watch_pub: function (e) { //点击公共区头像选择视频
    // console.log(e.currentTarget.dataset.id);
    this.setData({ selectedpub_idx: e.currentTarget.dataset.id, selected_idx:'t'});
    var p = [that.data.p1, that.data.p2, that.data.p3]
    that.setData({ camera_address: p[e.currentTarget.dataset.id] });//设置此时的摄像头组
    that.setData({ address_index: 0, now_adr: that.data.camera_address[0] });//设置默认选中地址为数组里第一个
    return
    // if (e.currentTarget.dataset.id=="11")
    // {
    //   this.setData({ camera_address: that.data.p1});//设置此时的摄像头组
    // }
    // if (e.currentTarget.dataset.id == "22") {
    //   this.setData({ camera_address: that.data.p2 });//设置此时的摄像头组
    // }
    // if (e.currentTarget.dataset.id == "33") {
    //   this.setData({ camera_address: that.data.p3});//设置此时的摄像头组
    // }
    // that.setData({ address_index: 0 });//设置默认选中地址为数组里第一个
  },
  get_shop_msg: function () {

    app.req("get_shop.html", {}, function (res) {
      var shop_arr = [];
      if (res.status == "success") {
        for (var shop in res.mess) {
          shop_arr.push(res.mess[shop]);

        }

        that.setData({ shopIndex: shop_arr });
        that.setData({ objectShop: res.mess });
      } else if (res.status == "error") {
        app.toast_up(res.mess)
      } else {
        app.modal("错误提示", "网络错误，是否重试？", function (re) {
          if (re) {
            that.get_service_msg()
          }
        })
      }
    })
  },
})